﻿using Jiguang.JPush;
using Jiguang.JPush.Model;
using System;
using System.Collections.Generic;
using System.Net;

namespace JPushDemo
{
    /// <summary>
    /// 极光推送
    /// </summary>
    public class JPushHelper
    {
        private static string _appKey = "6c24b1c0ba0429b4f356bcdd";
        private static string _masterSecret = "0875e881bebe902f5d660dce";

        /// <summary>
        /// 推送通知
        /// </summary>
        /// <param name="msg">JPushMsgBody，消息体</param>
        /// <returns>JPushReult</returns>
        public static JPushReult SendNotification(JPushMsgBody msg)
        {
            if (msg == null)
            {
                return JPushReult.Fail("参数为null");
            }

            var pushMsg = new PushPayload
            {
                CId = Guid.NewGuid().ToString("N"),
                Platform = msg.PlatformList,
                Audience = msg.RegistrationIdList,
                Notification = new Notification
                {
                    Alert = msg.Notification?.Content,
                    Android = new Android
                    {
                        Alert = msg.Notification?.Content,
                        Title = msg.Notification?.Title,
                        Extras = msg.Notification?.ExtraContent == null ? null : new Dictionary<string, object> { { ((int)msg.Notification.ExtraContent.ExtraType).ToString(), msg.Notification.ExtraContent.ExtraMsg } }
                    },
                    IOS = new IOS
                    {
                        Alert = msg.Notification?.Content,
                        Extras = msg.Notification?.ExtraContent == null ? null : new Dictionary<string, object> { { ((int)msg.Notification.ExtraContent.ExtraType).ToString(), msg.Notification.ExtraContent.ExtraMsg } }
                    }
                },
                Message = new Message
                {
                    Content = msg.Message?.Content,
                    Title = msg.Message?.Title,
                    Extras = msg.Message?.ExtraContent == null ? null : new Dictionary<string, object> { { ((int)msg.Message.ExtraContent.ExtraType).ToString(), msg.Message.ExtraContent.ExtraMsg } }
                },
                Options = new Options
                {
                    IsApnsProduction = true,
                    TimeToLive = 86400 * 3
                },
                InappMessage = new InappMessage { IsNotice = true }
            };
            var client = new JPushClient(_appKey, _masterSecret);
            try
            {
                var response = client.SendPush(pushMsg);
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return JPushReult.Success();
                }
                else
                {
                    return JPushReult.Fail(response.Content);
                }
            }
            catch(Exception e)
            {
                return JPushReult.Fail(e.Message);
            }
        }
    }
}
